package io.renren.modules.redis;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @Author: 蔡翔
 * @Date: 2019/7/3 15:07
 * @Version 1.0
 */
@Service
public class RedisService {
    @Autowired
    JedisPool jedisPool;



    /*获取当前对象*/
    public <T> T get(KeyPrefix keyPrefix,String key,Class<T> clazz){
        Jedis jedis = null; //因为是一个连接池 用完千万不要忘记 释放掉
        try {
            jedis = jedisPool.getResource(); //取得连接后，就可以操作数据库了
            //生成真正的Key
            String realKey = keyPrefix.getPrefix()+key;

            String str = jedis.get(realKey);
            T t = stringToBean(str,clazz);
            return t;
        }finally {
            returnToPool(jedis);
        }
    }

    /*设置对象*/
    public <T> boolean set(KeyPrefix keyPrefix,String key,T value){
        Jedis jedis = null; //因为是一个连接池 用完千万不要忘记 释放掉
        try {
            jedis = jedisPool.getResource(); //取得连接后，就可以操作数据库了
            //生成真正的Key
            String realKey = keyPrefix.getPrefix()+key;

            String str = beanToString(value);
            if(str == null || str.length() <=0){
                return false;
            }
            //生成真正的Key
            int seconds = keyPrefix.expireSecond();
            if(seconds<=0){ //说明这个redis 是永久的
                jedis.set(realKey,str);
            }else {  //否则有时限
                //jedis.setnx() todo 1-8 11:25
                jedis.setex(realKey,seconds,str);
            }


            return true;
        }finally {
            returnToPool(jedis);
        }
    }

    public static <T> String beanToString(T value) {
        if(value ==null){
            return null;
        }
        Class<?> clazz = value.getClass(); //根据class 来判断 value 的类型
        if(clazz == int.class || clazz == Integer.class){
            return ""+value;
        }else if(clazz == String.class){
            return (String)value;
        }else if(clazz == long.class || clazz == Long.class){
            return ""+value;
        }else {
            return JSON.toJSONString(value);
        }
    }

    public static <T> T stringToBean(String str,Class<T> clazz) {
        if(str ==null || str.length() == 0 || clazz ==null){
            return null;
        }
        if(clazz == int.class || clazz == Integer.class){
            return (T)Integer.valueOf(str);
        }else if(clazz == String.class){
            return (T)str;
        }else if(clazz == long.class || clazz == Long.class){
            return (T)Long.valueOf(str);
        }else {
            return JSON.toJavaObject(JSON.parseObject(str),clazz);
        }
    }

    private void returnToPool(Jedis jedis) {
        if(jedis != null){
            jedis.close(); //这里的close 知识把这个连接给关闭了，并且把连接资源释放到连接池。
        }
    }

    /*判断Key是否存在*/
    public <T> boolean exits(KeyPrefix prefix,String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String realKey = prefix.getPrefix() + key;
            return jedis.exists(realKey);
        }finally {
            returnToPool(jedis); //释放连接。
        }
    }

    //增加Key
    public <T> Long incr(KeyPrefix prefix,String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //生成真正的Key
            String realKey = prefix.getPrefix() + key;
            return jedis.incr(realKey);
        }finally {
            returnToPool(jedis);
        }
    }

    //减少Key
    //如 1. set key 123; 2. decr key   ==> 结果 122
    public <T> Long decr(KeyPrefix prefix,String key){
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            //生成真正的Key
            String realKey = prefix.getPrefix() + key;
            return jedis.decr(realKey);
        }finally {
            returnToPool(jedis);
        }
    }



}
